<?php
/**
 * Created by PhpStorm.
 * User: Mloong
 * Date: 2018/11/30
 * Time: 15:40
 */

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Models\Goods;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use App\Http\Models\Columninfo;
use App\Http\Models\User;
use App\Http\Models\UserSubmitAudit;

class ColumninfoController extends Controller
{
    protected $headerStr = '统计';
    protected $onlinetime = '2018-11-16 00:30:00';

    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->body($this->grid());
            Columninfo::delOtherData();
        });
    }
    protected function grid()
    {
        return Admin::grid(Columninfo::class, function (Grid $grid) {
            $onlinetime = $this->onlinetime;
            $users = User::where('created_at', '>=', $onlinetime)->get();
            if ($users) {
                $users = $users->toArray();
            } else {
                $users = [];
            }
            $userIds = array_column($users, 'id');
            $grid->new_user('新注册用户')->display(function () use ($onlinetime) {
                $res = User::where('created_at', '>=', $onlinetime)->count();
                return $res;
            });
            $grid->send_order('放单用户')->display(function () use ($onlinetime,$userIds) {
                $res = Goods::where('created_at', '>', $onlinetime)->whereIn('id', $userIds)->groupBy('userid')->count();
                return $res;
            });
            $grid->send_goods('发的商品')->display(function () use ($onlinetime,$userIds) {
                $res = Goods::where('created_at', '>', $onlinetime)->whereIn('userid', $userIds)->count();
                return $res;
            });
            $grid->apply_send_goods('申请放单用户')->display(function () use ($onlinetime) {
                $res = UserSubmitAudit::where('created_at', '>=', $onlinetime)->count();
                return $res;
            });
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();//禁用查询过滤器
            });
            $grid->disableExport();
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->actions(function ($actions) {
                if (method_exists($actions, 'disableView')) {
                    $actions->disableView();
                }
            });
            $grid->paginate(env('Admin_Limit'));
        });
    }
}